﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;

namespace TridentReport
{
    public partial class observationlog : Form
    {
        WebBrowser edds;
        public DataTable tridenttable = new DataTable();
        DataRow trow;
        public static grid newrecords;
        int rowcount;        
        private string sid;
        private string dl;
        WebBrowser observation = new WebBrowser();             
        DateTime today = DateTime.Today;
        DateTime threshold;
        DateTime constrstart;
        string str;
        string seq;
        string tstrseq;
        bool firstpass = false;
        int tr;
        
        public observationlog(DataTable trident, WebBrowser eddswebpage, int i)
        {
            tr = i;
            edds = eddswebpage;
            tridenttable = trident;
            InitializeComponent();
            openobservation();
        }

        private void openobservation()
        {
            HtmlElement table = edds.Document.GetElementById("UserIDTable");
            HtmlElementCollection devlog = table.GetElementsByTagName("td");
            string devlogid = devlog[7].InnerHtml;
            int a = devlogid.IndexOf("SID=");
            int b = devlogid.IndexOf("');");
            sid = devlogid.Substring(a + 4, b - (a + 4));
            HtmlElement devlogin = edds.Document.GetElementById(sid);
            dl = "https://www.bldgportal.com/DeviationLog/LoginDirect.aspx?SID=" + sid;
            observation.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(observation_DocumentCompleted);
            observation.AllowNavigation = true;
            observation.Navigate(dl, false);
        }

        void observation_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (firstpass == false)
            {
                firstpass = true;
                tridenttable = MySqlConn.trident.Tables["trident"];
                rowcount = tridenttable.Rows.Count;
                
                threshold = today.AddDays(-730);
                DataColumn open = tridenttable.Columns.Add("open");               
                
                for (tr = 0; tr < rowcount; tr++)
                {
                    trow = tridenttable.Rows[tr];
                    str = trow["str_num"].ToString();
                    string csd = trow["constr_start_act"].ToString();
                    if (csd != " ")
                    {
                        constrstart = Convert.ToDateTime(trow["constr_start_act"]);

                        if (constrstart > threshold && constrstart < today.AddDays(-30) && str != "3418")
                        {
                            newpage(tridenttable, tr);
                        }
                    }
                }                
            }

            else if (firstpass == true)
            {
                edds.Dispose();
                tridenttable.Dispose();
                trow = null;
                newrecords = null;
                rowcount = 0;
                sid = null;
                dl = null;
                observation.Dispose();        
                this.Close();
            }                        
        }        

        public static void newpage(DataTable trident, int i)
        {            
            newrecords = new grid(trident, i);
            newrecords.ShowDialog();
        }       
    }
}
